﻿using IShoppingBll;
using IShoppingDal;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingBll
{
	public class RoleInfoBll:BaseDeleteBll<RoleInfo>, IRoleInfoBll
	{
		private IRoleInfoDal _roleInfoDal;
		private IUserInfo_RoleInfoDal _userInfo_RoleInfoDal;
		private IMenuInfo_RoleInfoDal _menuInfo_RoleInfoDal;
		public RoleInfoBll(IRoleInfoDal roleInfoDal
			, IUserInfo_RoleInfoDal userInfo_RoleInfo
			, IMenuInfo_RoleInfoDal menuInfo_RoleInfoDal) :base(roleInfoDal)
		{
			_roleInfoDal = roleInfoDal;
			_userInfo_RoleInfoDal = userInfo_RoleInfo;
			_menuInfo_RoleInfoDal = menuInfo_RoleInfoDal;
		}
		/// <summary>
		/// 添加角色
		/// </summary>
		/// <param name="roleInfo"></param>
		/// <returns></returns>
		public bool AddRoleInfo(RoleInfo roleInfo)
		{
			return _roleInfoDal.Add(roleInfo);
		}

		/// <summary>
		/// 绑定菜单
		/// </summary>
		/// <param name="roleInfoId"></param>
		/// <param name="menuInfoIds"></param>
		public void BindMenuInfo(string roleInfoId, List<string> menuInfoIds)
		{
			DateTime now = DateTime.Now;

			//当前角色已绑定的用户信息
			var roleInfo_MenuInfos = _menuInfo_RoleInfoDal.GetEntitiesDb().Where(r => r.RoleId == roleInfoId).ToList();

			//先删除已绑定的
			foreach (var item in roleInfo_MenuInfos)
			{
				if (!menuInfoIds.Contains(item.MenuId))
				{
					//userInfoIds不存在的用户id就删除
					_menuInfo_RoleInfoDal.Delete(item.ID);
				}
			}

			//添加
			foreach (var item in menuInfoIds)
			{
				//如果已经存在的用户就不添加，不存在的才添加
				if (!roleInfo_MenuInfos.Any(a => a.MenuId == item))
				{
					MenuInfo_RoleInfo r_RoleInfo_MenuInfo = new MenuInfo_RoleInfo()
					{
						ID = Guid.NewGuid().ToString(),
						CreateTime = now,
						RoleId = roleInfoId,
						MenuId = item
					};
					_menuInfo_RoleInfoDal.Add(r_RoleInfo_MenuInfo);
				}
			}
		}



		/// <summary>
		/// 绑定用户
		/// </summary>
		/// <param name="roleInfoId"></param>
		/// <param name="userInfoIds"></param>
		public void BinUserInfo(string roleInfoId, List<string> userInfoIds)
		{
			DateTime dateTime = DateTime.Now;
			//当前角色已绑定的用户信息
			var U_UserInfo_RoleInfoDal = _userInfo_RoleInfoDal.GetEntitiesDb().Where(r => r.RoleID == roleInfoId).ToList();

			//删除绑定的用户数据
			foreach (var item in U_UserInfo_RoleInfoDal)
			{
				if (!userInfoIds.Contains(item.UserId))
				{
					//userInfoIds不存在的用户id就删除
					_userInfo_RoleInfoDal.Delete(item.ID);
				}
			}

			//添加绑定
			foreach (var item in userInfoIds)
			{
				//如果已经存在的用户就不添加，不存在的才添加
				if (!U_UserInfo_RoleInfoDal.Any(a => a.UserId == item))
				{
					UserInfo_RoleInfo r_UserInfo_RoleInfo = new UserInfo_RoleInfo()
					{
						ID = Guid.NewGuid().ToString(),
						RoleID = roleInfoId,
						UserId = item
					};
					_userInfo_RoleInfoDal.Add(r_UserInfo_RoleInfo);
				}
			}
		}


		/// <summary>
		/// 删除角色
		/// </summary>
		/// <param name="roleInfoId"></param>
		/// <returns></returns>
		public bool DeleteRoleInfo(string roleInfoId)
		{
			RoleInfo roleInfo = _roleInfoDal.GetEntity(roleInfoId);
			if(roleInfo.RoleName==UserInfo.UserInfoRoleInfoEnum.商家.ToString()|| roleInfo.RoleName == UserInfo.UserInfoRoleInfoEnum.用户.ToString() || roleInfo.RoleName == UserInfo.UserInfoRoleInfoEnum.配送员.ToString())
            {
				return false;
            }
            else
            {
				int count  = _userInfo_RoleInfoDal.GetEntitiesDb().Where(r => r.RoleID.Contains(roleInfoId)).Count();
                if (count > 0)
                {
					return false;
                }
                else
                {
					bool isSuccess = _roleInfoDal.Delete(roleInfoId);
                    if (isSuccess)
                    {
						return true;
                    }
                    else
                    {
						return false;
                    }
                }


            }

		}



		/// <summary>
		/// 获取当前角色已绑定的菜单id集合
		/// </summary>
		/// <param name="roleInfoId"></param>
		/// <returns></returns>
		public List<string> GetBindMenuIds(string roleInfoId)
		{
			//查询当前角色已绑定的用户id
			return _menuInfo_RoleInfoDal.GetEntitiesDb().Where(r => r.RoleId == roleInfoId).Select(r => r.MenuId).ToList();
		}



		public List<string> GetBindUserIds(string roleInfoId)
		{
			//查询当前角色已绑定的用户id
			return _userInfo_RoleInfoDal.GetEntitiesDb().Where(r => r.RoleID == roleInfoId).Select(r => r.UserId).ToList();
		}



		/// <summary>
		/// (分页)获取角色集合
		/// </summary>
		/// <param name="page"></param>
		/// <param name="limit"></param>
		/// <param name="count"></param>
		/// <param name="RoleName"></param>
		/// <returns></returns>
		public object GetRoleInfoByPage(int page, int limit, out int count, string RoleName)
		{
			//获取数据库中用户全部没删除的数据（未真实查询）
			var roleInfos = _roleInfoDal.GetEntitiesDb().Where(u => u.IsDelete == false);

			if (!string.IsNullOrEmpty(RoleName))
			{
				roleInfos = roleInfos.Where(u => u.RoleName.Contains(RoleName));
			}

			//查询出来数据的数量
			count = roleInfos.Count();

			//分页
			roleInfos = roleInfos.OrderBy(r => r.CreateTime).Skip((page - 1) * limit).Take(limit);

			var list = roleInfos.ToList().Select(u =>
			{
				return new
				{
					u.ID,
					u.RoleName,
					u.Describe,
					CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
				};
			});

			return list;
		}


		/// <summary>
		/// 判断当前角色是否存在
		/// </summary>
		/// <param name="roleName"></param>
		/// <returns></returns>
		public bool IsHadRoleInfo(string roleName)
		{
			int count = _roleInfoDal.GetEntitiesDb().Where(r => r.RoleName == roleName && !r.IsDelete).Count();
			return count > 0;
		}


		/// <summary>
		/// 更新角色信息
		/// </summary>
		/// <param name="roleInfoId"></param>
		/// <param name="RoleName"></param>
		/// <param name="Describe"></param>
		/// <returns></returns>
		public bool UpdateRoleInfo(string roleInfoId, string RoleName, string Describe)
		{
			RoleInfo roleInfo = _roleInfoDal.GetEntity(roleInfoId);
			if (roleInfo != null)
			{
				roleInfo.RoleName = RoleName;
				roleInfo.Describe = Describe;

				return _roleInfoDal.Update(roleInfo);
			}
			else
			{
				return false;
			}
		}
	}
}
